﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using BMallDBAccess;
using ServiceStack.Redis;

namespace TKWeb
{
	public class Global : System.Web.HttpApplication
	{
		private static PooledRedisClientManager s_RedisPool;

		protected void Application_Start(object sender, EventArgs e)
		{

		}

		protected void Session_Start(object sender, EventArgs e)
		{

		}

		protected void Application_BeginRequest(object sender, EventArgs e)
		{

		}

		protected void Application_AuthenticateRequest(object sender, EventArgs e)
		{

		}

		protected void Application_Error(object sender, EventArgs e)
		{

		}

		protected void Session_End(object sender, EventArgs e)
		{

		}

		protected void Application_End(object sender, EventArgs e)
		{

		}

		private static MySqlDBAccess s_db;
		public static MySqlDBAccess DBAccess
		{
			get
			{
				if (s_db == null)
				{
					s_db = MySqlDBAccessManager.GetAccess("spider", "chinavb234123489");
					//s_db = new MySqlDBAccess("Server=124.160.149.71;Database=spider;User Id=spider;Password=spider;port=3306;connection lifetime=15;Connection Timeout=;min pool size=1;max pool size=30;charset=utf8;");
				}
				return s_db;
			}
		}

		public static int UserId
		{
			get
			{
				if (System.Web.HttpContext.Current.Session["UserId"] == null)
				{
					return 0;
				}
				return Convert.ToInt32(System.Web.HttpContext.Current.Session["UserId"]);
			}

			set
			{
				System.Web.HttpContext.Current.Session["UserId"] = value;
			}
		}

		public static RedisClient GetRedisClient()
		{
			if (s_RedisPool == null)
			{
				s_RedisPool = new PooledRedisClientManager(new string[] { System.Configuration.ConfigurationSettings.AppSettings["redis_connect"] });
			}

			return (RedisClient)s_RedisPool.GetClient();
		}
	}
}